<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>查询口碑</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div>
        <el-form :model="formData" ref="myform" label-width="100px" class="demo-ruleForm">
          <el-form-item label="来源" prop="source">
            <el-radio-group v-model="formData.source">
              <el-radio label="0">全部</el-radio>
              <el-radio label="1">毕业生</el-radio>
              <el-radio label="2">朋友</el-radio>
              <el-radio label="3">同事</el-radio>
              <el-radio label="4">网络</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="姓名" prop="username">
            <el-input v-model="formData.username"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="phone">
            <el-input v-model="formData.phone"></el-input>
          </el-form-item>

          <el-form-item prop="inputtime" label="报备时间">
            <el-date-picker
              v-model="formData.inputtime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('myform')">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="box-card" style="margin-top:20px">
      <div slot="header" class="clearfix">根据查询条件共有 {{total}} 条结果</div>
      <div>
        <el-table :data="tableData" style="width: 100%" v-loading="loading">
          <el-table-column prop="key" label="ID" width="100"></el-table-column>
          <el-table-column prop="username" label="姓名" width="180"></el-table-column>
          <el-table-column prop="phone" label="手机" width="180"></el-table-column>
          <el-table-column prop="inputtime" label="报备时间" width="200">
            <template slot-scope="scope">{{scope.row.inputtime | formateDate}}</template>
          </el-table-column>
          <el-table-column prop="source" label="来源" width="120">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.source === '1'">{{scope.row.source | formateSource}}</el-tag>
              <el-tag
                v-if="scope.row.source === '2'"
                type="success"
              >{{scope.row.source | formateSource}}</el-tag>
              <el-tag
                v-if="scope.row.source === '3'"
                type="info"
              >{{scope.row.source | formateSource}}</el-tag>
              <el-tag
                v-if="scope.row.source === '4'"
                type="danger"
              >{{scope.row.source | formateSource}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="message" label="备注"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <!-- 点击编辑按钮，需要把当前这条信息的id传给跳转的那个页面 -->
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="$router.push({path:'/index/kb-edit', query: { id: scope.row.id}})"
              >编辑</el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="del(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="page">
          <!-- layout, 显示的内容：prev 上一页； next 下一页； pager 页码列表； sizes 每页多少条， jumper 跳转第n页-->
          <el-pagination
            background
            layout="total, prev, pager, next, sizes, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 30, 50]"
          ></el-pagination>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import request from "@/api/koubei";
import "@/utils/filters.js";
export default {
  data() {
    return {
      formData: {
        username: "",
        phone: "",
        inputtime: "",
        source: "0"
      },

      loading: false,
      tableData: [], // 查询的结果
      curr: 1, // 当前第几页
      limit: 10, // 每页多少条
      total: 0, // 查询的总条数
    };
  },
  methods: {
    // ajax查询方法
    getSearchList() {
      request
        .searchKoubei({ curr: this.curr, limit: this.limit, ...this.formData })
        .then((r) => {
          console.log(r.data);
          if (r.data.code === 0) {
            // 添加成功的提示
            this.$message({
              type: "success",
              message: "查询成功",
            });

            r.data.list.forEach((item, index) => {
              item.key = index + 1 + (this.curr - 1) * this.limit;
            });

            // 把查询的结果赋值给tableData
            this.tableData = r.data.list;
            // 把查询的总条数赋值给total
            this.total = r.data.total;
          } else {
            this.$message.error(r.data.msg);
          }
        });
    },

    // 查询
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.formData.inputtime);
          this.curr = 1;
          this.limit = 10;
          this.getSearchList();

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 事件：改变当前第几页
    handleCurrentChange(val) {
      this.curr = val;
      this.getSearchList();
    },

    // 事件：改变每页多少条
    handleSizeChange(val) {
      this.limit = val;
      this.getSearchList();
    },
  },
};
</script>

<style>
</style>